<template>
  <el-container>
    <el-header>
      <el-row>
        <el-col :span="2">
          <img src="../assets/heima.png" alt="" />
        </el-col>
        <el-col :span="10">
          <span class="header-text">电商后台管理系统</span>
        </el-col>
        <el-col class="header-row-btn" :span="12">
          <el-button type="info" @click="quit">退出</el-button>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <!-- 导航栏 -->
      <el-aside :width="isCollpase ? '64px' : '200px'">
        <!-- 折叠菜单栏 -->
        <div class="fold-menu" @click="clickFoldMenu">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#3390FF"
          unique-opened
          :collapse="isCollpase"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级导航 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuListData"
            :key="item.id"
          >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="MenuItemPath('/' + subitem.path)"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{ subitem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuListData: [],
      // 一级菜单图标
      icons: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单栏折叠展开
      isCollpase: false,
      // 当前二级菜单的Path
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出按钮
    quit() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuListData = res.data
      // console.log(this.menuListData)
    },
    // 控制菜单栏折叠与展开
    clickFoldMenu() {
      this.isCollpase = !this.isCollpase
    },
    // 二级菜单当前path
    MenuItemPath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  padding-left: 0;
  .el-row {
    display: flex;
    align-items: center;
    .header-row-btn {
      text-align: right;
    }
    .header-text {
      font-size: 20px;
      color: #fff;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
  .fold-menu {
    background-color: #4a5064;
    text-align: center;
    font-size: 16px;
    padding: 3px 0;
    color: #fff;
    letter-spacing: 2px;
    cursor: pointer;
  }
}

.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
